package cn.zuo.view;

import cn.zuo.pojo.Message;
import cn.zuo.pojo.User;
import cn.zuo.service.Check;
import cn.zuo.utils.PPKeyFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
/**
 * @Author 白白旧维
 * @Date 2021/7/7 11:13
 */
public class LoginJFrame {
    private JFrame jFrame;

    public JFrame getjFrame() {
        return jFrame;
    }

    public void setjFrame(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    private JPanel mjp;
    private JTextField username;
    private JPasswordField password;
    private JLabel usernameLabel;
    private JLabel passwordLabel;
    private JButton btn_clear;
    private JButton btn_register;
    private JButton btn_login;
    private Socket s;

    public LoginJFrame() {
        btn_clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                JOptionPane.showMessageDialog(null,"清空!!");
                username.setText("");
                password.setText("");
            }
        });

        btn_register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                JOptionPane.showMessageDialog(null,"注册!!");
                JFrame jFrame = new JFrame();

                RegisterJFrame registerJFrame = new RegisterJFrame(jFrame);
                jFrame.setContentPane(registerJFrame.getMjp());
                jFrame.pack();
                jFrame.setLocation(500,400);
                jFrame.setTitle("注册界面  <©2021 白白旧维>");
                jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jFrame.setVisible(true);

            }
        });
        btn_login.addActionListener(new ActionListener() {
            @SneakyThrows
            @Override
            public void actionPerformed(ActionEvent e) {
//                JOptionPane.showMessageDialog(null,"登录!!");
                if(!verifyLogin()){
                    return;
                }

                try {
                    Check check = new Check();
                    boolean isOk = check.check(username.getText().trim(), String.valueOf(password.getPassword()).trim());
                    s = check.getS();

                    if(!isOk){
                        return;
                    }

                    Vector<String> vector = new Vector<>();


                    Message msg = check.getMsg();
                    String strNames = msg.getMsg();
                    List<String> names = JSONArray.parseArray(strNames, String.class);

                    for (String name ://展示用户列表
                            names) {
                        vector.add(name);
                    }

                    JFrame jFrame1 = new JFrame();

                    //旧：登录成功生成自己的密钥
//                    Map<String, Long> key = PPKeyFactory.getOneKey(System.currentTimeMillis() + new Random().nextLong());


                    UserMain userMain = new UserMain(check.getMsg().getReceiver(),s,vector,check.getKey());
                    jFrame1.setContentPane(userMain.getMjp());
                    jFrame1.pack();
                    jFrame1.setLocation(500,400);
                    jFrame1.setTitle("用户主界面  <©2021 白白旧维>");
                    jFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jFrame1.setVisible(true);

                    jFrame.setVisible(false);

                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null,"服务器未开启或出错!!");
                    ex.printStackTrace();
                }

            }
        });
    }

    public JPanel getMjp() {
        return mjp;
    }

    public static void main(String[] args) throws Exception {
        //Nimbus风格
        String lookAndFeel = "javax.swing.plaf.nimbus.NimbusLookAndFeel";
        UIManager.setLookAndFeel(lookAndFeel);

        JFrame jFrame = new JFrame();

        LoginJFrame loginJFrame = new LoginJFrame();
        loginJFrame.setjFrame(jFrame);

        jFrame.setContentPane(loginJFrame.getMjp());
        jFrame.setLocation(800,300);
        jFrame.setTitle("登录界面  <©2021 白白旧维>");
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
//        JFrame jFrame = new JFrame();
//
//        dogeo d = new dogeo();
//        jFrame.setContentPane(d.getPanel1());
//
//        jFrame.setTitle("ddoge");
//        jFrame.setLocation(300,300);
//        jFrame.setVisible(true);
//        jFrame.pack();
//        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    }


    public boolean verifyLogin(){

        if("".equals(username.getText().trim())){
            JOptionPane.showMessageDialog(null,"请输入账号");
            return false;
        }
        if("".equals(String.valueOf(password.getPassword()).trim())||password.getPassword().length==0){
            JOptionPane.showMessageDialog(null,"请输入密码");
            return false;
        }

        return true;
    }


}
